package com.ranyk.www.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ranyk.www.entity.Role;
import com.ranyk.www.entity.UserRole;
import com.ranyk.www.mapper.UserRoleMapper;
import com.ranyk.www.pojo.dto.AdminUserPageDTO;
import com.ranyk.www.pojo.dto.UserRoleDTO;
import com.ranyk.www.pojo.vo.AdminUserVO;
import com.ranyk.www.service.IUserRoleService;
import com.ranyk.www.support.page.PageInfo;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * CLASS_NAME: UserRoleServiceImpl.java <br/>
 *
 * @author ranyk           <br/>
 * @version V1.0           <br/>
 * @decription: 管理员角色关联表 业务逻辑层接口实现  <br/>
 * @date: 2022-04-03 <br/>
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    private UserRoleMapper userRoleMapper;

    @Autowired
	public void setUserRoleMapper(UserRoleMapper userRoleMapper) {
		this.userRoleMapper = userRoleMapper;
	}

	@Override
    public UserRoleDTO getByUserId(Long userId, Integer status) {

        List<Role> roles = userRoleMapper.listByUserId(userId, status);

        Set<String> roleSigns = new HashSet<>();
        Set<String> roleIds = new HashSet<>();

        // 遍历角色列表
        for (Role role : roles) {
            roleSigns.add(role.getRoleSign());
            roleIds.add(role.getRoleId().toString());
        }
        return new UserRoleDTO(roleSigns, roleIds);
    }

    @Override
    public List<Role> listByUserId(Long userId, Integer status) {
        return userRoleMapper.listByUserId(userId, status);
    }

    @Override
    public AdminUserPageDTO listByRoleId(Long roleId, PageInfo pageInfo, String search) {
        Page<AdminUserVO> page = new Page<>(pageInfo.getCurrent(), pageInfo.getLimit());
        RowBounds rowBounds = new RowBounds(pageInfo.getCurrent(), pageInfo.getLimit());
        List<AdminUserVO> userVOList = userRoleMapper.listByRoleId(roleId, pageInfo, search, rowBounds);
        pageInfo.setTotal(Long.valueOf(page.getTotal()).intValue());
        return new AdminUserPageDTO(userVOList, pageInfo);
    }
}
